(function($) {
	"use strict";

	$.widget("isperp.baiduMap", {
		options : {

			refreshTimeout : 99,
			refreshTimeoutLabel : null,
			centerPointLng : 0,
			centerPointLat : 0,
			zoom : 13,
			map : null,
			showUserPoint : true,
			showIncidentPoint : true,
			showCiPoint : true

		},
		_create : function() {
			var self = this, o = self.options, element = self.element[0];

			var c_point = new BMap.Point(o.centerPointLng, o.centerPointLat);

			o.map = new BMap.Map(element.id);
			o.map.centerAndZoom(c_point, o.zoom);
			o.map.addControl(new BMap.NavigationControl());
			o.map.enableScrollWheelZoom();
			o.map.enableContinuousZoom();

			o.map.addControl(new BMap.ScaleControl()); // 添加默认比例尺控件

			o.map.addControl(new BMap.MapTypeControl({
				anchor : BMAP_ANCHOR_TOP_RIGHT
			}));

			var timeoutIntervalCount = o.refreshTimeout, $timeOutLabel = $("#" + o.refreshTimeoutLabel);
			self.refreshPoint();
			var timeoutSetInterval = function() {
				timeoutIntervalCount--;

				if (timeoutIntervalCount != 0) {
					$timeOutLabel.text(timeoutIntervalCount);
					return;
				}

				timeoutIntervalCount = o.refreshTimeout;
				self.refreshPoint();
			};

			window.setInterval(timeoutSetInterval, 1000);
			o.map.addEventListener("dragging", function showInfo() {
				//layer.closeAll('tips');
			});

			o.map.addEventListener("zoomstart", function showInfo() {
				//layer.closeAll('tips');
			});
		},
		clearAllPoint : function() {

			this.options.map.clearOverlays();
		},

		addUserPoint : function(map, userInfo) {

			var myCompOverlay = new ComplexUserOverlay(map, userInfo);
			map.addOverlay(myCompOverlay);

			$("#s_" + userInfo.name).raty({
				readOnly : true,
				score : 4.26
			});
		},
		addCiPoint : function(map, ciInfo) {
			var myCompOverlay = new ComplexCiOverlay(map, ciInfo, this.options);
			map.addOverlay(myCompOverlay);

			$("#s_" + ciInfo.id).raty({
				readOnly : true,
				score : 4.26
			});
		},
		addIncidentPoint : function(map, incidentInfo) {
			var label_opts = {
				position : new BMap.Point(incidentInfo.lng, incidentInfo.lat), // 指定文本标注所在的地理位置
				offset : new BMap.Size(-10, -25)
			};
			var bgColor = "";

			switch (incidentInfo.level) {
			case "1":
				bgColor = "url(img/map/markers.png) no-repeat 0 0";
				break;
			case "2":
				bgColor = "url(img/map/markers.png) no-repeat -21px 0";
				break;
			case "3":
				bgColor = "url(img/map/markers.png) no-repeat -42px 0";
				break;
			case "4":
				bgColor = "url(img/map/markers.png) no-repeat -63px 0";
				break;
			case "5":
				bgColor = "url(img/map/markers.png) no-repeat -84px 0";
				break;

			}

			var label = new BMap.Label(" ", label_opts); // 创建文本标注对象
			label.setStyle({
				color : "#FFFFFF",
				fontSize : "12px",
				height : "16px",
				lineHeight : "16px",
				fontFamily : "微软雅黑",
				background : bgColor,
				border : "none",
				padding : "9px 11px 8px 10px",
				margin : "0",
				cursor : "pointer"

			});
			label.addEventListener("click", function() {
				isperp.openDialog({
					contentUrl : "workflow/processRun/entry?processKey=incidentProcess&id=0000029b78161192004d"
				});
			});

			label.addEventListener("mouseover", function() {
				label.setStyle({
					backgroundPositionY : "-34px"
				});
			});

			label.addEventListener("mouseout", function() {
				label.setStyle({
					backgroundPositionY : "0px"
				});
			});
			map.addOverlay(label);
		},
		refreshPoint : function() {
			var self = this, o = self.options;
			self.clearAllPoint();
			if (o.showUserPoint) {
				$.get("map/baidu/listUsers", function(data) {
					$(data).each(function(index, item) {
						self.addUserPoint(o.map, item);
					});
				});

			}
			if (o.showCiPoint) {
				$.get("map/baidu/listCis", function(data) {
					var ci_total = data.length;
					var ci_outStatus = 0;
					$(data).each(function(index, item) {
						self.addCiPoint(o.map, item);
						if (item.status == "离线")
							ci_outStatus = ci_outStatus + 1;
					});
					if ($("#ci_total").attr("id") != undefined) {
						$("#ci_total").text(ci_total);
					}
					if ($("#ci_outStatus").attr("id") != undefined) {
						$("#ci_outStatus").text(ci_outStatus);
					}

				});

			}
			if (o.showIncidentPoint) {
				self.addIncidentPoint(o.map, {
					lng : 113.305857000000,
					lat : 23.124490000000,
					level : "3"
				});

				self.addIncidentPoint(o.map, {
					lng : 113.302857000000,
					lat : 23.144490000000,
					level : "1"
				});

				self.addIncidentPoint(o.map, {
					lng : 113.315857000000,
					lat : 23.104490000000,
					level : "2"
				});
			}

		},
		showUserPoint : function(v) {
			this.options.showUserPoint = v;
		},
		showIncidentPoint : function(v) {
			this.options.showIncidentPoint = v;
		},
		showCiPoint : function(v) {
			this.options.showCiPoint = v;
		}

	});
}(jQuery));

function ComplexUserOverlay(map, userInfo) {

	var point = new BMap.Point(userInfo.longitude, userInfo.latitude);
	this._point = point;
	this._userInfo = userInfo;

}
ComplexUserOverlay.prototype = new BMap.Overlay();
ComplexUserOverlay.prototype.initialize = function(map) {

	var self = this;
	self._map = map;

	var bgColor;
	var border;

	bgColor = "#fff";
	border = "1px solid #BC3B3A";

	if (self._photo) {

	}

	var div = this._div = document.createElement("div");
	div.style.position = "absolute";
	div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
	div.style.backgroundColor = bgColor;
	div.style.border = border;
	div.style.color = "white";
	div.style.width = "136px";
	div.style.height = "38px";
	div.style.padding = "2px";
	div.style.lineHeight = "18px";
	div.style.whiteSpace = "nowrap";
	div.style.MozUserSelect = "none";
	div.style.fontSize = "12px"
	div.zIndex = 2;

	div.onclick = function() {
		isperp.openDialog({
			title : self._userInfo.fullname + "的任务列表",
			contentUrl : "workflow/processRun/myTaskListView?username=" + self._userInfo.name
		});
	}

	var lDiv = document.createElement("div"), rDiv = document.createElement("div"), rDiv1 = document.createElement("div"), rDiv2 = document.createElement("div");
	var lImg = document.createElement("img");

	lDiv.style.width = "40px";
	lDiv.style.float = "left";
	lDiv.appendChild(lImg);
	lImg.setAttribute("src", this._userInfo.photo);
	lImg.style.width = "36px";
	lImg.style.height = "38px";

	rDiv.style.float = "right";
	rDiv.style.width = "95px";

	rDiv1.style.color = "#000000";
	rDiv1.style.fontWeight = "700";

	rDiv1.appendChild(document.createTextNode(self._userInfo.fullname + "(" + self._userInfo.channelId + ")"));
	rDiv2.setAttribute("id", "s_" + self._userInfo.name);
	rDiv.appendChild(rDiv1);
	rDiv.appendChild(rDiv2);

	div.appendChild(lDiv);
	div.appendChild(rDiv);

	var arrow = self._arrow = document.createElement("div");
	arrow.style.background = "url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png) no-repeat";
	arrow.style.position = "absolute";
	arrow.style.width = "11px";
	arrow.style.height = "10px";
	arrow.style.top = "42px";
	arrow.style.left = "10px";
	arrow.style.overflow = "hidden";
	div.appendChild(arrow);

	map.getPanes().labelPane.appendChild(div);

	return div;
}
ComplexUserOverlay.prototype.draw = function() {
	var map = this._map;
	var pixel = map.pointToOverlayPixel(this._point);
	this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
	this._div.style.top = pixel.y - 50 + "px";
}

// -------加入CI显示--------//
function ComplexCiOverlay(map, ciInfo, options) {

	// 获取经纬度 pv:IP|192.168.2.3,纬度|36.32323044,经度|36.33323044
	var tpv = ciInfo.pv;
	var longitude = "";// 经度
	var latitude = "";// 纬度
	var ip = "";
	var exPropertys = tpv.split(",");
	for ( var i = 0; i < exPropertys.length; i++) {
		if (exPropertys[i].indexOf('经度') > -1) {
			longitude = (exPropertys[i].split("|"))[1];
		}
		if (exPropertys[i].indexOf("纬度") > -1) {
			latitude = (exPropertys[i].split("|"))[1];
		}
		if (exPropertys[i].indexOf("IP") > -1) {
			ip = (exPropertys[i].split("|"))[1];
		}
	}

	var point = new BMap.Point(longitude, latitude);
	this._point = point;
	this._ciInfo = ciInfo;
	this._options = options;
	this._ip = ip;
}
ComplexCiOverlay.prototype = new BMap.Overlay();
ComplexCiOverlay.prototype.initialize = function(map) {
	var self = this;
	self._map = map;

	var bgColor;
	var border;

	bgColor = "#fff";
	// border = "1px solid #BC3B3A";
	var div = this._div = document.createElement("div");
	div.style.position = "absolute";
	div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
	//div.style.backgroundColor = bgColor;
	div.style.border = border;
	div.style.color = "white";
	// div.style.width = "136px";
	div.style.height = "38px";
	div.style.padding = "2px";
	div.style.lineHeight = "18px";
	div.style.whiteSpace = "nowrap";
	div.style.MozUserSelect = "none";
	div.style.fontSize = "12px"
	div.zIndex = 2;

	div.onclick = function() {
		var codeHtml = "<a href=\"javascript:viewCi('" + self._ciInfo.id + "')\">" + self._ciInfo.code + "</a>";

		var statusHtml = "<span style='color:red'>" + self._ciInfo.status + "</span>";
		if (self._ciInfo.status == "在线" || self._ciInfo.status == "在用") {
			statusHtml = "<span style='color:green'>" + self._ciInfo.status + "</span>";
		}

		var tispDiv = "<div style='color:black'><ul><li><span>编号：</span>" + codeHtml + "</li><li><span>IP：</span><span>" + self._ip + "</span></li><li><span>状态：</span>" + statusHtml + "</li><li><span>位置：</span><span>" + self._ciInfo.address + "</span></li><li><span>维保商：</span><span>金山科技有限公司</span></li></ul></div>";
		/*layer.tips(tispDiv, $(div), {
			tipsMore : false,
			tips : [ 4, '#ffffff' ],
			time : 10000,
			area : 'auto',
			maxWidth : 400

		});*/
	}

	var lDiv = document.createElement("div"), rDiv = document.createElement("div"), rDiv1 = document.createElement("div"), rDiv2 = document.createElement("div");
	var lImg = document.createElement("img");

	lDiv.style.width = "40px";
	lDiv.style.float = "left";
	lDiv.appendChild(lImg);
	if (self._ciInfo.status == '离线') {
		lImg.setAttribute("src", "img/map/outstatus.png");
	} else if (self._ciInfo.status == '在线') {
		lImg.setAttribute("src", "img/map/instatus.png");
	}

	//lImg.style.width = "36px";
	//lImg.style.height = "38px";

	rDiv.style.float = "right";
	rDiv.style.width = "95px";
	rDiv.style.display = "none";

	rDiv1.style.color = "#000000";
	rDiv1.style.fontWeight = "700";

	// rDiv.setAttribute("class","test1");

	rDiv1.appendChild(document.createTextNode(self._ciInfo.name + "(" + self._ciInfo.id + ")"));
	rDiv2.setAttribute("id", "s_" + self._ciInfo.id);
	rDiv.appendChild(rDiv1);
	rDiv.appendChild(rDiv2);

	div.appendChild(lDiv);
	// div.appendChild(rDiv);

	var arrow = self._arrow = document.createElement("div");
	arrow.style.background = "url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png) no-repeat";
	arrow.style.position = "absolute";
	arrow.style.width = "11px";
	arrow.style.height = "10px";
	arrow.style.top = "42px";
	arrow.style.left = "10px";
	arrow.style.overflow = "hidden";
	div.appendChild(arrow);

	map.getPanes().labelPane.appendChild(div);

	return div;
}
ComplexCiOverlay.prototype.draw = function() {
	var map = this._map;
	var pixel = map.pointToOverlayPixel(this._point);
	this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
	this._div.style.top = pixel.y - 50 + "px";
}

function viewCi(id) {
	isperp.openDialog({
		title : "查看",
		contentUrl : "configuration/ci/edit?id=" + id + "&readonly=true"
	});
}
// -------加入CI显示 END--------//
